package com.blog.controller;

import com.blog.entity.BlogComment;
import com.blog.service.CommentService;
import com.blog.utils.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @Author Huangzp
 * @Date 2022/1/7 16:38
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     *
     */
    @ResponseBody
    @GetMapping(value = "{articleId}")
    public HashMap<String, Object> getComment(@PathVariable(value = "articleId") int articleId){
        HashMap<String,Object> result = new HashMap<>();
        List<BlogComment> comment =commentService.getCommentsByArticleId(articleId);
        result.put("data",comment);
        return result;
    }

    /**
     * 新增评论
     * @param comment 评论内容
     * @return result
     */
    @ResponseBody
    @PostMapping("/save")
    public HashMap<String, Object> saveComment(BlogComment comment){
        HashMap<String,Object> result = new HashMap<>();
        //新增评论
        commentService.saveComment(comment);
        return result;
    }

    /**
     * 删除评论
     * @param id  评论id
     * @param articleByManagerId  作者id
     * @return result
     */
    @ResponseBody
    @DeleteMapping("/delete")
    public HashMap<String, Object> deleteComment(Integer id, Integer articleByManagerId){
        HashMap<String,Object> result = new HashMap<>();
        //删除评论
        commentService.deleteComment(id,articleByManagerId);
        return result;
    }

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public HashMap<String, Object> getCommentPage(BlogComment comment, Integer pageSize, Integer currentPage){
        HashMap<String,Object> result = new HashMap<>();
        if (pageSize != null && currentPage != null){
            PageHelper.startPage(currentPage, pageSize);
            List<BlogComment>  comments= commentService.getCommentPage(comment);
            PageInfo<BlogComment> pageInfo = new PageInfo<>(comments);
            result.put("data",pageInfo);
        }else {
            List<BlogComment>  comments= commentService.getCommentPage(comment);
            result.put("data",comments);
        }
        return result;
    }

    /**
     * 通过id删除
     */
    @DeleteMapping("/deletes")
    public HashMap<String, Object> delComment(Integer[] ids) throws BusinessException {
        HashMap<String,Object> result = new HashMap<>();
        commentService.delComment(Arrays.asList(ids));
        return result;
    }
}
